package com.example.book_recommend.controller;

import com.example.book_recommend.pojo.VueResultInfo;
import com.example.book_recommend.pojo.userInfo.VueBookListInfo;
import com.example.book_recommend.pojo.userInfo.VueCartInfo;
import com.example.book_recommend.pojo.userInfo.VueCartListInfo;
import com.example.book_recommend.service.BookService;
import com.example.book_recommend.service.CartService;
import com.example.book_recommend.utils.ResultFactory;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@CrossOrigin
@RequestMapping("cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    BookService bookService;

    @RequestMapping("/addBook")
    @ApiOperation(value = "添加书籍", notes = "invokePost说明", httpMethod = "POST")
    public VueResultInfo addBook(@RequestBody VueCartInfo cartInfo, BindingResult bindingResult){
        return ResultFactory.buildSuccessResult(cartService.addBook(cartInfo.getUid(), Long.valueOf(cartInfo.getBookId()), cartInfo.getCount()));
    }

    @RequestMapping("/delBook")
    @ApiOperation(value = "删除书籍", notes = "invokePost说明", httpMethod = "POST")
    public VueResultInfo delBook(@RequestBody VueCartInfo cartInfo, BindingResult bindingResult){
        return ResultFactory.buildSuccessResult(cartService.delBook(cartInfo.getUid(), Long.valueOf(cartInfo.getBookId()), cartInfo.getCount()));
    }

    @RequestMapping("/search")
    @ApiOperation(value = "查看对应用户的购物车", notes = "invokePost说明", httpMethod = "POST")
    public VueResultInfo searchByUID(@RequestBody VueCartInfo cartInfo, BindingResult bindingResult){
        return ResultFactory.buildSuccessResult(cartService.searchByUID(cartInfo.getUid()));
    }

    @RequestMapping("/searchN&U")
    @ApiOperation(value = "查看对应用户的购物车对应的书", notes = "invokePost说明", httpMethod = "POST")
    public VueResultInfo searchNU(@RequestBody VueBookListInfo bookList){
        return ResultFactory.buildSuccessResult(cartService.searchNU(bookList.getBookIds()));
    }

    @RequestMapping("/directPay")
    @ApiOperation(value = "直接付款，不走购物车", notes = "invokePost说明", httpMethod = "POST")
    public VueResultInfo directPay(@RequestBody VueCartInfo cartInfo, BindingResult bindingResult){
        return ResultFactory.buildSuccessResult(cartService.directPay(cartInfo.getUid(), Long.valueOf(cartInfo.getBookId()), cartInfo.getCount()));
    }

    @RequestMapping("/payWithCart")
    @ApiOperation(value = "购物车付款", notes = "invokePost说明", httpMethod = "POST")
    public VueResultInfo payWithCart(@RequestBody VueCartListInfo vueCartListInfo, BindingResult bindingResult){
        return ResultFactory.buildSuccessResult(cartService.payWithCart(vueCartListInfo.getCartIds()));
    }
}
